<?php

class TruongController {

    public function invoke() {
        $dir = dirname(__FILE__);

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }

        // tác vụ
        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }
   
        if ($action == 'remove') {
            // mảng danh sách giá trị id
            $values = $_POST["id"];
 
            $result = $this->deleteTruong($values);
           
            
            if ($result == OK) {
//                Utility::messageBox('Xóa thành công');
                 echo'OK';
            
            }
             exit();
        } else if ($action == 'add') {
            $BEP_ID=$_POST['BEP_ID'];
            $TRUONG_TEN_TRUONG=$_POST['TRUONG_TEN_TRUONG'];
            $DS_LOAIHOAHONG=$_POST['dshoahong'];
            $result = $this->addTruong($BEP_ID, $TRUONG_TEN_TRUONG,$DS_LOAIHOAHONG);
            if ($result == OK) {
                if ($action == 'add') {
                    echo 'OK';
                }
            }
             exit();
             
            
        } else if ($action == 'edit' ) {

            
            $TRUONG_ID=$_POST['TRUONG_ID'];
            $BEP_ID=$_POST['BEP_ID'];
            $TRUONG_TEN_TRUONG=$_POST['TRUONG_TEN_TRUONG'];
           
            $DS_LOAIHOAHONG=$_POST['dshoahong'];
            
            $result1 = $this->deleteHoaHong($TRUONG_ID);

            $result = $this->updateTruong($TRUONG_ID,$BEP_ID, $TRUONG_TEN_TRUONG,$DS_LOAIHOAHONG);
                        
            if ($result == OK) {
                if ($action == 'edit') {
                    echo 'OK';
                } 
            }
             exit();
        }


        if ($task == 'add') {
            $xtemplate = new XTemplate('./components' . DS . 'com_truong' . DS . 'views' . DS . 'truong_add.php');
            
            $beps=$this->getAllBep();
            
              if ($beps != null) {
                 
                foreach ($beps as $bep) {

                     
                     $xtemplate->assign('BEP_ID', $bep->getBEP_ID());
                    $xtemplate->assign('BEP_TEN_BEP', $bep->getBEP_TEN_BEP());
                    $xtemplate->parse('main.bep');
                    
                }
            }
            $loaihoahongs=$this->getAllLoaiHoaHong();
            
              if ($loaihoahongs != null) {
                 
                foreach ($loaihoahongs as $loaihoahong) {

                     
                     $xtemplate->assign('LOAIHOAHONG_ID', $loaihoahong->getLHH_ID());
                    $xtemplate->assign('LOAIHOAHONG_TEN', $loaihoahong->getLHH_TEN_LOAI());
                    $xtemplate->parse('main.loaihoahong');
                    
                }
            }
            
            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
        // nếu task là edit thì include trang thêm vào
        else if ($task == 'edit') {
            $id = $_GET['idtruong'];
            $xtemplate = new XTemplate('./components' . DS . 'com_truong' . DS . 'views' . DS . 'truong_edit.php');
            $truong = $this->getTruongById($id);
//            Utility::messageBox($id);

            if ($truong != null) {
                
 
                    $xtemplate->assign('TRUONG_ID', $truong->getTRUONG_ID());
                    $xtemplate->assign('BEP_ID', $truong->getBEP_ID());
                    $xtemplate->assign('TRUONG_TEN_TRUONG', $truong->getTRUONG_TEN_TRUONG());
                    
                     $beps=$this->getAllBep();
             $option_bep='';
              if ($beps != null) {
                 
                foreach ($beps as $bep) {

                     
                      if($truong->getBEP_ID()==$bep->getBEP_ID()){
                          $option_bep .="<option selected=selected value='".$bep->getBEP_ID()."'>".$bep->getBEP_TEN_BEP()."</option>";
                          }else{
                             $option_bep .="<option  value='".$bep->getBEP_ID()."'>".$bep->getBEP_TEN_BEP()."</option>";
                            
                          }
                    
                }
            }
                    
                    
                    $xtemplate->assign('BEP', $option_bep);
                    
                 $arrayhoahongs = $this->getArrayHoaHongById($truong->getTRUONG_ID());    
                     if ($arrayhoahongs != null) {
                         $index=0;
                foreach ($arrayhoahongs as $arrayhoahong) {
                    $xtemplate->assign('INDEX', $index);
                    $xtemplate->assign('ARRAY_HOAHONG', $arrayhoahong->getLHH_ID());
                    $xtemplate->parse('main.arrayhoahong');
                    $index++;
                }
                    
                
                }
                 
                 
                    
              $loaihoahongs=$this->getAllLoaiHoaHong();
             $hoahongs=null;
              if ($loaihoahongs != null) {
                 $option_hoahong='';
                 
                foreach ($loaihoahongs as $loaihoahong) {
                    
                    $hoahongs = $this->getHoaHongById($loaihoahong->getLHH_ID(),$truong->getTRUONG_ID());
                  
                    if($hoahongs == null ){
                        
                       $option_hoahong .='<input  onchange="addloaihoahong(this.value,this.checked);" value="'.$loaihoahong->getLHH_ID().'" name="checkbox-mini-'.$loaihoahong->getLHH_ID().'" id="checkbox-mini-'.$loaihoahong->getLHH_ID().'" class="custom" data-mini="true" type="checkbox" >
                       <label for="checkbox-mini-'.$loaihoahong->getLHH_ID().'">'.$loaihoahong->getLHH_TEN_LOAI().'</label>';     
                    
                        
                    }else{
                        
                       $option_hoahong .='<input  onchange="addloaihoahong(this.value,this.checked);" value="'.$loaihoahong->getLHH_ID().'" name="checkbox-mini-'.$loaihoahong->getLHH_ID().'" id="checkbox-mini-'.$loaihoahong->getLHH_ID().'" class="custom" data-mini="true" type="checkbox" checked="true">
                       <label for="checkbox-mini-'.$loaihoahong->getLHH_ID().'">'.$loaihoahong->getLHH_TEN_LOAI().'</label>';     
                     
                    }
                }
            }
                    
                     $xtemplate->assign('HOAHONG', $option_hoahong);
                    $xtemplate->parse('main.truong');
                   
                
            }




            $xtemplate->parse('main');
            $xtemplate->out('main');
            
            
        } 
        else {
            $viewFile = $dir . '/views/truong.php';
            $xtemplate = new XTemplate($viewFile);
            $truongs = $this->getAllTruong();


            if ($truongs != null) {
                foreach ($truongs as $truong) {

                     $xtemplate->assign('TRUONG_ID', $truong->getTRUONG_ID());
                    $xtemplate->assign('BEP', $this->getBepById($truong->getBEP_ID())->getBEP_TEN_BEP());
                    $xtemplate->assign('TRUONG_TEN_TRUONG', $truong->getTRUONG_TEN_TRUONG());
                    
                    
                    
                    
                    $xtemplate->parse('main.truong');
                   
                }
            }




            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
        
        
        
        
        
        
        
        
    }

    private function getAllTruong() {
        $db = new Database();
        try {
            $truong= new Truong();

            $result = $truong->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
  private function getTruongById($id) {
        $db = new Database();
        try {
            $truong = new Truong();
            $truong->setTRUONG_ID($id);
            $result = $truong->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
     private function getHoaHongById($id,$truong) {
        $db = new Database();
        try {
            $hoahong = new Hoa_hong();
            $result = $hoahong->getByID($id,$truong,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    private function getArrayHoaHongById($truong) {
        $db = new Database();
        try {
            $hoahong = new Hoa_hong();
            $result = $hoahong->getArrayByID($truong,$db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
   
     private function  getAllLoaiHoaHong() {
        $db = new Database();
        try {
            $loaihoahong = new LoaiHoaHong();

            $result = $loaihoahong->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
     private function getAllBep() {
        $db = new Database();
        try {
            $bep = new Bep();

            $result = $bep->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
      private function getBepById($id) {
        $db = new Database();
        try {
            $bep = new Bep();
            $bep->setBEP_ID($id);
            $result = $bep->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
    private function addTruong($BEP_ID, $TRUONG_TEN_TRUONG,$DS_LOAIHOAHONG){
        
         $db = new Database();
        try {            
           
                $truong = new Truong();
                $truong->setBEP_ID($BEP_ID);
                 $truong->setTRUONG_TEN_TRUONG($TRUONG_TEN_TRUONG);
                $truong->insert($db);
                $myArray = explode(',', $DS_LOAIHOAHONG);
                for ($i = 0; $i < sizeof($myArray); $i++) {
                 $Hoa_hong=new Hoa_hong();
                $Hoa_hong->setTRUONG_ID($truong->getTRUONG_ID());
                $Hoa_hong->setLHH_ID($myArray[$i]);
                $Hoa_hong->insert($db);
            }
                
                
               
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
        
    }
    
        private function updateTruong($TRUONG_ID,$BEP_ID, $TRUONG_TEN_TRUONG,$DS_LOAIHOAHONG){
        
         $db = new Database();
        try {            
           
                $truong = new Truong();
                 $truong->setTRUONG_ID($TRUONG_ID);
                $truong->setBEP_ID($BEP_ID);
                 $truong->setTRUONG_TEN_TRUONG($TRUONG_TEN_TRUONG);
                $truong->update($db);

                 $myArray = explode(',', $DS_LOAIHOAHONG);
                 
                 
                for ($i = 0; $i < sizeof($myArray); $i++) {
                    if(($myArray[$i]!='') || ($myArray[$i])!=null){
                        $Hoa_hong=new Hoa_hong();
                $Hoa_hong->setTRUONG_ID($TRUONG_ID);
                $Hoa_hong->setLHH_ID($myArray[$i]);
                $Hoa_hong->insert($db);
                    }
                 
                }
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
        
    }
    
    
    
    private function deleteHoaHong($values) {
        $db = new Database();
        try {            
           
                $hoahong = new Hoa_hong();
                $hoahong->setTRUONG_ID($values);
                $hoahong->delete($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
        private function deleteTruong($values) {
        $db = new Database();
        try {            
           
                $truong = new Truong();
                $truong->setTRUONG_ID($values);
                $truong->delete($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
    
    
    
    
}
